# Create your views here.

from django.http import HttpResponse
from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
from django.template import RequestContext
from django import forms

import register_user
import login_user
import forms as myforms

def LandingPage(request):
    return HttpResponseRedirect('/login/')

def LoginView(request):
    if request.method == 'POST':
        form = myforms.LoginForm(request.POST)
	if form.is_valid():
            username = form.cleaned_data['username']
            password = form.cleaned_data['password']
            (status, message) = login_user.AuthenticateUser(request, username, password)
            if status is True and message == 'logged in':
                return HttpResponseRedirect('/home/')
            else:
                return HttpResponse(message)
    else:
        login_form = myforms.LoginForm()

    return render_to_response('login_register/login.html', { 'login_form' : login_form },
			      context_instance=RequestContext(request))


def RegisterView(request):
    if request.method == 'POST':
        form = myforms.RegisterForm(request.POST)
	if form.is_valid():
            username = form.cleaned_data['username']
            email = form.cleaned_data['email']
            password = form.cleaned_data['password']
            password_repeat = form.cleaned_data['password_repeat']
            if password == password_repeat:
                if register_user.AddUser(username, email, password) == True:
                    return HttpResponse('Account created')
		else:
                    return HttpResponse('Account creation error')
    else:
        form = myforms.RegisterForm()

    return render_to_response('login_register/register.html', { 'register_form' : form },
			      context_instance=RequestContext(request))


def LogoutView(request):
    login_user.LogoutUser(request)
    return HttpResponseRedirect('/login/')
